//reload刷新页面，
//newLoad获取最新数据并刷新页面
//getAllClaims获取理赔信息
//delOneClaims删除一个理赔订单信息
import '@css/adminPage.css'

window.addEventListener("DOMContentLoaded", async () => {

  newLoad()
  // document.querySelector("#rightList").innerHTML
  document.querySelector("#rightTop").innerHTML = `
  <p id="searchClaimBox">请输入用户名进行搜索<input id="searchClaimBox"><button id="searchClaimBtn">点击搜索</button></p>
  <div id="addClaimBox"><button id="addClaimBtn">+新增一个理赔申请</button></div>
  `
  //用户信息管理
  document.querySelector('#userListBtn').addEventListener('click', () => {
    location = `userPage.html`
  })

})

//理赔页面专属所用
document.querySelector("body").addEventListener("click", async (e) => {
  // 删除单个保单
  if (e.target.className == "claimdelete") {
    delOneClaims(e.target.parentElement.parentElement.firstElementChild.innerHTML)
  }
  // 修改单个保单
  if (e.target.className == "claimmodify") {
    //渲染页面
    await modifyClaim(e.target.parentElement.parentElement.firstElementChild.innerHTML)
    document.querySelector("#uploadVoucher").addEventListener("change", async function () {
      console.log(this.files[0]);
      const { code, message, avatarPath } = await avatarUploadApi(this.files[0])
      if (code) {
        // console.log(avatarPath);
        document.querySelector("#showVoucherImg").setAttribute("src", `http://127.0.0.1:3000/images/${avatarPath}`)

      }

    })
  }
  if (e.target.id == "makeSureClaim") {
    const result = await modifyContent()
    alert("修改成功")
    newLoad()
  }

  //模糊搜索事件绑定
  if (e.target.id == "searchClaimBtn") {
    let content = document.querySelector("#searchClaimBox").value
    const { rows } = await searchClaim(content)

    let newArr = rows.filter(item => {
      return !item.delete
    })
    reload(newArr)
  }

  //增加功能区
  if (e.target.id == "addClaimBtn") {
    addClaimFirst()
    document.querySelector("#uploadVoucher").addEventListener("change", async function () {
      console.log(this.files[0]);
      const { code, message, avatarPath } = await avatarUploadApi(this.files[0])
      if (code) {
        // console.log(avatarPath);
        document.querySelector("#showVoucherImg").setAttribute("src", `http://127.0.0.1:3000/images/${avatarPath}`)

        alert(document.querySelector("#showVoucherImg").getAttribute("src"))
      }

    })
  }

  //确认增加按钮
  if (e.target.id == "makeSureAddClaim") {
    const result = await addClaimContent()
    alert("增加成功")
    newLoad()
  }

  //取消执行操作按钮
  if (e.target.id == "cancelClaim") {
    newLoad()
  }

  //跳转到首页
  if (e.target.className == "title") {
    window.location.href = "adminPage.html"
  }
  //审核#claimdetail
})
